<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Setup extends CI_Controller {

	/**
	* 协议
	*/
	public function index(){
		$this->load->view('index');
	}

	public function environment(){
        $_SESSION['error'] = FALSE;
		$this->load->view('environment',array(
            'env_vars'=>$this->installer->check_environment(),
            'dirs' => $this->installer->check_dirs_and_files(),
            'functions'=> $this->installer->check_functions(),
        ));
	}

    public function setting(){
        $this->load->helper(array('form'));
        $this->load->view('setting');
    }

    public function installing(){

        $this->load->helper(array('form','url'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('db[host]',lang('database_server'),'required');
        $this->form_validation->set_rules('db[name]',lang('database_name'),'required');
        $this->form_validation->set_rules('db[user]',lang('database_user'),'required');
        //$this->form_validation->set_rules('db[password]','数据库授权密码','required');
        $this->form_validation->set_rules('db[prefix]',lang('database_table_prefix'),'required');
        $this->form_validation->set_rules('admin[user]',lang('admin_user'),'required');
        $this->form_validation->set_rules('admin[password]',lang('admin_password'),'required');
        $this->form_validation->set_rules('admin[email]',lang('admin_email'),'required');
        if ($this->form_validation->run() == FALSE){
            $this->load->view('setting',array('has_errors'=>true));
        } else {
            $_SESSION['error'] = NULL;
            $this->_output($this->load->view('installing','',true));
            $db = $this->input->post('db');
            $dns = "{$db['driver']}://{$db['user']}:{$db['password']}@{$db['host']}?char_set=utf8&dbcollat=utf8_general_ci";
            $this->load->database($dns);
            $this->installer->create_tables($db['name'],$db['prefix']);

            //注册创始人帐号
            $admin = $this->input->post('admin');
            $auth = $this->installer->build_auth_key();
            $this->installer->register_administrator($db['prefix'], $admin,$auth);

            //创建配置文件
            $this->installer->write_config($db, $auth);

            if (empty($_SESSION['error'])) {
                $this->_redirect('setup/completed',3);
            }

        }
    }

    public function completed(){
        $this->load->view('completed');
    }

    private function _redirect($url, $time=0, $msg='') {
        $url = site_url($url);
        //多行URL地址支持
        $url        = str_replace(array("\n", "\r"), '', $url);
        if (!headers_sent()) {
            // redirect
            if (0 === $time) {
                header('Location: ' . $url);
            } else {
                header("refresh:{$time};url={$url}");
                echo($msg);
            }
            exit();
        } else {
            $str    = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
            if ($time != 0)
                $str .= $msg;
            exit($str);
        }
    }

    public function _output($output)
    {
        echo $output;
    }

}